/**
 * Copyright (c) 2010
 * Bert Young. UESTC. 
 */

#ifndef BERT_TCPSERVER_H
#define BERT_TCPSERVER_H

#include "BaseServer.h"
#include "ServerSocket.h"

class TCPTaskPool;

class TCPServer : public BaseServer
{
	/**
	 * The listen socket, throw new connfd into tcptaskpool
	 */
	ServerSocket * listenSocket;
	/**
	 * Server calls this function continuously until done
	 */
	void listenForeign();
protected:
	/**
	 * Do this before server exit
	 */
	void recycle()
	{
		delete listenSocket;
		listenSocket = NULL;
	}
	/**
	 * The tcptaskpool, process every connection
	 */
	TCPTaskPool * pool;
	/**
	 * Create new tcptask(redefine msgparse func) and call pool->addVerify
	 */
	virtual void newTCPTask(int connfd, struct sockaddr_in * remote ) = 0;
	/**
	 * Create listen socket, bind port and listen
	 */
	bool bind(const in_port_t port);
public:
	/**
	 * Constructor
	 */
	TCPServer(const std::string & name = "TCPServer");
	/**
	 * Destructor
	 */
	virtual ~TCPServer();
};

#endif

